const basePage = require('../../common/BasePage')
import {
  areaData
} from '../../config/index';
Page({
  behaviors: [basePage],
  /**
   * 页面的初始数据
   */
  data: {
    areaData: areaData,
    areaPickerVisible: false,
    store: {},
    sid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      store: wx.getStorageSync('store')
    })
    this.data.sid = this.data.store.sid
    delete this.data.store.sid;
    console.log('store1', this.data.store);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  onPickArea() {
    this.setData({
      areaPickerVisible: true
    });
  },
  area_change(e) {
    console.log('area_change', e);
    let selectedOptions = e.detail.selectedOptions;
    this.setData({
      'store.province': selectedOptions[0].label,
      'store.city': selectedOptions[1].label,
      'store.districtCode': selectedOptions[2].value,
      'store.district': selectedOptions[2].label,
      areaPickerVisible: false,
    });
  },
  builtInSearch({
    code,
    name
  }) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting[code] === false) {
            wx.showModal({
              title: `获取${name}失败`,
              content: `获取${name}失败，请在【右上角】-小程序【设置】项中，将【${name}】开启。`,
              confirmText: '去设置',
              confirmColor: '#FA550F',
              cancelColor: '取消',
              success(res) {
                if (res.confirm) {
                  wx.openSetting({
                    success(settinRes) {
                      if (settinRes.authSetting[code] === true) {
                        resolve();
                      } else {
                        console.warn('用户未打开权限', name, code);
                        reject();
                      }
                    },
                  });
                } else {
                  reject();
                }
              },
              fail() {
                reject();
              },
            });
          } else {
            resolve();
          }
        },
        fail() {
          reject();
        },
      });
    });
  },
  onSearchAddress() {
    this.chooseLocation();
  },
  onInputValue(e) {
    const {
      item
    } = e.currentTarget.dataset;
    const {
      value = ''
    } = e.detail;

    this.data.store[item] = value
    console.log(this.data.store);
  },
  chooseLocation() {
    wx.chooseLocation({
      success: (res) => {
        console.log("选择位置", res);
        this.setData({
          "store.address": res.name,
          "store.latitude": res.latitude, //
          "store.longitude": res.longitude,
        })

      },
      fail: function (res) {
        console.warn(`wx.chooseLocation fail: ${JSON.stringify(res)}`);
        if (res.errMsg !== 'chooseLocation:fail cancel') {
          wx.showToast({
            title: JSON.stringify(res),

            icon: 'none',

          })

        }
      },
    });
  },
  formSubmit() {
    this.request({
      url: this.data.host + '/v1/stores/' + this.data.sid,
      method: "put",
      data: this.data.store,
      loadingTip: '修改...'
    }, res => {
      setTimeout(() => {
        wx.showToast({
          title: '修改成功',
          mask: true,
        })
      }, 100);
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,

        })
      }, 2000);

    })
  }
})